<?php
//购物车
class Controller_App_Cart extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/cart/detail";
        parent::__construct($req, $res);
    }
	
	//显示购物车
    public function action_index ()
    {
        $detailList = App_Cart::detailList($this->_user);
        if (empty($detailList))
            $this->template->set_filename('smarty:app/cart/empty');
        else
            $this->template->set('detailList', $detailList);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '购物车', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('block', 'cart');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }
    
	//删除购物车
	//$nid int 购物车id
    public function action_del ($nid)
    {
        App_Cart::remove($this->_user, array($nid));
        $this->request->redirect('/cart/index');
    }

	//添加到购物车
    public function action_add ()
    {
        $query = $this->request->query();
        $goodsId = Arr::get($query, 'goods');
        $attrId  = Arr::get($query, 'attr');
        $counts  = Arr::get($query, 'count');

        $ret = App_Cart::add($goodsId, $attrId, $counts);

        if ($this->request->is_ajax())
        {
            echo json_encode(array(
                'status' => 0,
                'message' => '',
            ));
            exit;
        }

        $this->request->redirect('/cart');
    }

	public function before ()
	{
		parent::before();
		if ($this->_uid == 0)
		{
                        if ($this->request->is_ajax())
                        {
                            echo json_encode(array(
                                'status' => 2,
                                'message' => '未登录',
                            ));
                            exit;
                        }else{
                            $this->request->redirect('/login');
                        }
		}
	}

}
